package thired.class08_tree;

import java.util.ArrayList;

public class Class10_MaxSubBSTHead {
    public static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int data) {
            this.value = data;
        }
    }

    public static int getBSTSize(Node head) {
        if (head == null) {
            return 0;
        }
        ArrayList<Node> arr = new ArrayList<>();
        in(head, arr);
        for (int i = 1; i < arr.size(); i++) {
            if (arr.get(i).value <= arr.get(i - 1).value) {
                return 0;
            }
        }
        return arr.size();
    }

    public static void in(Node head, ArrayList<Node> arr) {
        if (head == null) {
            return;
        }
        in(head.left, arr);
        arr.add(head);
        in(head.right, arr);
    }

    public static Node maxSubBSTHead1(Node head) {
        if (head == null) {
            return null;
        }
        if (getBSTSize(head) != 0) {
            return head;
        }
        Node leftAns = maxSubBSTHead1(head.left);
        Node rightAns = maxSubBSTHead1(head.right);
        return getBSTSize(leftAns) >= getBSTSize(rightAns) ? leftAns : rightAns;
    }

    public static Node maxSubBSTHead2(Node head) {
        if (head == null) {
            return null;
        }
        return process(head).maxSubBSTNode;
    }

    static class Info{
        public Node maxSubBSTNode;
        public int maxSubBSTSize;
        public int size;
        public int max;
        public int min;

        public Info(Node maxSubBSTNode,int maxSubBSTSize, int size, int max, int min) {
            this.maxSubBSTNode = maxSubBSTNode;
            this.maxSubBSTSize = maxSubBSTSize;
            this.size = size;
            this.max = max;
            this.min = min;
        }
    }

    public static Info process(Node root){
        if(root == null){
            return  null;
        }

        Info left = process(root.left);
        Info right = process(root.right);

        int max = root.value;
        int min = root.value;
        int maxSubBSTSize = 0;
        Node node = null;
        if (left != null) {
            min = Math.min(min, left.min);
            max = Math.max(max, left.max);
            node = left.maxSubBSTNode;
            maxSubBSTSize = left.maxSubBSTSize;
        }
        if (right != null) {
            min = Math.min(min, right.min);
            max = Math.max(max, right.max);
            if (right.maxSubBSTSize > maxSubBSTSize) {
                node = right.maxSubBSTNode;
                maxSubBSTSize = right.maxSubBSTSize;
            }
        }
        int size = 1;
        if (left != null){
            size += left.size;
        }
        if(right != null){
            size += right.size;
        }

        if(
                (left == null || left.maxSubBSTSize == left.size)
                        && (right == null || right.maxSubBSTSize == right.size)
                        && (left == null || left.max < root.value)
                        && (right == null || right.min > root.value)
        ){
            int p1 = left == null ? 0 : left.maxSubBSTSize;
            int p2 = right == null ? 0 : right.maxSubBSTSize;
            maxSubBSTSize = Math.max((p1 + p2 + 1),maxSubBSTSize);
            node = root;
        }


        return new Info(node,maxSubBSTSize,size,max,min);
    }

    // for test
    public static Node generateRandomBST(int maxLevel, int maxValue) {
        return generate(1, maxLevel, maxValue);
    }

    // for test
    public static Node generate(int level, int maxLevel, int maxValue) {
        if (level > maxLevel || Math.random() < 0.5) {
            return null;
        }
        Node head = new Node((int) (Math.random() * maxValue));
        head.left = generate(level + 1, maxLevel, maxValue);
        head.right = generate(level + 1, maxLevel, maxValue);
        return head;
    }

    public static void main(String[] args) {
        int maxLevel = 4;
        int maxValue = 100;
        int testTimes = 1000000;
        for (int i = 0; i < testTimes; i++) {
            Node head = generateRandomBST(maxLevel, maxValue);
            if (maxSubBSTHead1(head) != maxSubBSTHead2(head)) {
                System.out.println("Oops!");
            }
        }
        System.out.println("finish!");
    }
}
